package com.aaa.controller;

import javax.validation.Valid;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.aaa.commons.result.PageInfo;
import com.aaa.model.CharacterResult;
import com.aaa.service.ICharacterResultService;
import com.aaa.commons.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenjian
 * @since 2021-11-25
 */
@Controller
@RequestMapping("/characterResult")
public class CharacterResultController extends BaseController {

    @Autowired private ICharacterResultService characterResultService;

    @GetMapping("/manager")
    public String manager() {
        return "admin/characterResult/characterResultList";
    }

    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(CharacterResult characterResult, Integer page, Integer rows, String sort,String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        EntityWrapper<CharacterResult> ew = new EntityWrapper<CharacterResult>(characterResult);
        Page<CharacterResult> pages = getPage(page, rows, sort, order);
        pages = characterResultService.selectPage(pages, ew);
        pageInfo.setRows(pages.getRecords());
        pageInfo.setTotal(pages.getTotal());
        return pageInfo;
    }

    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/characterResult/characterResultAdd";
    }

    /**
     * 添加
     * @param
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Valid CharacterResult characterResult) {
        boolean b = characterResultService.insert(characterResult);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        CharacterResult characterResult = new CharacterResult();
        boolean b = characterResultService.updateById(characterResult);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        CharacterResult characterResult = characterResultService.selectById(id);
        model.addAttribute("characterResult", characterResult);
        return "admin/characterResult/characterResultEdit";
    }

    /**
     * 编辑
     * @param
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@Valid CharacterResult characterResult) {
        boolean b = characterResultService.updateById(characterResult);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
}
